package com.cloud.convertor;

import com.cloud.entity.CloudLogRecord;
import com.cloud.entity.HbaseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Component
public class CloudLogRecordConvertor {

    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

    /**
     * 将拦截的日志对象集合转换成插入hbase的对象集合
     * @param records
     * @return List
     */
    public List<HbaseEntity> convertList(List<CloudLogRecord> records) {
        if (CollectionUtils.isEmpty(records)) {
            return null;
        }

        List<HbaseEntity> results = new ArrayList<>();
        records.forEach(record -> {
            HbaseEntity hbaseEntity = new HbaseEntity();
            hbaseEntity.setId(record.getId());
            hbaseEntity.setReqUser(record.getReqUser());
            hbaseEntity.setAcctType(record.getAcctType());
            hbaseEntity.setSource(record.getSourceApplicationName());
            hbaseEntity.setTarget(record.getTargetApplicationName());
            if (record.getReqTime() != null) {
                String format = sdf.format(record.getReqTime());
                hbaseEntity.setReqTime(format);
            }
            hbaseEntity.setType(record.getReqMethod());
            hbaseEntity.setAddr(record.getReqAddr());
            hbaseEntity.setSkId(record.getSkyTraceId());
            hbaseEntity.setTId(record.getTraceId());
            results.add(hbaseEntity);
        });
        return results;
    }
}
